# coding: utf-8

from leancloud import Object
from leancloud import File
from leancloud import Query
from leancloud import LeanCloudError
from leancloud import User
from flask import request
from datetime import datetime
import time
import os
from werkzeug.utils import secure_filename
import json


class MeetItem(Object):
    pass


class Meeting():
    def __init__(self):
        pass

    # 获取最新的说说
    def update(self):
        room = request.json['roomid']
        orderman = request.json['orderman']
        timefrom = request.json['timefrom']
        timeto = request.json['timeto']
        hc = request.json['hc']
        building = request.json['building']
        meeting = MeetItem()
        meeting.set('room',room)
        meeting.set('orderman',orderman)
        meeting.set('timefrom',timefrom)
        meeting.set('timeto',timeto)
        meeting.set('hc',hc)
        meeting.set('building',building)
        meeting.save()
        data = {'code': 200, 'msg': 'successful', 'desc': ""}
        print "update: "
        return json.dumps(data, indent=1)
        pass

    def query(self):
        building = request.json['building']
        try:
            meetingItems = Query(MeetItem).equal_to('building',building).descending('createdAt').find()
        except LeanCloudError, e:
            if e.code == 101:  # 服务端对应的 Class 还没创建
                farmerSayItems = []
            else:
                raise e
        count = 0;
        resultItems = []
        for meetingItem in meetingItems:
            if(count == 1):
                break
            count +=1
            resultItem = {}
            resultItem['room'] = meetingItem.get('room')
            resultItem['orderman'] = meetingItem.get('orderman')
            resultItem['timefrom'] = meetingItem.get('timefrom')
            resultItem['timeto'] = meetingItem.get('timeto')
            resultItem['hc'] = meetingItem.get('hc')
            resultItem['building'] = meetingItem.get('building')
            resultItems.append(resultItem)
        data = {'code': 200, 'msg': 'successful', 'desc': "","meetingItems":resultItems}
        print "getStates: "
        return json.dumps(data, indent=1)
        pass

    def queryGet(self):
        try:
            meetingItems = Query(MeetItem).descending('createdAt').find()
        except LeanCloudError, e:
            if e.code == 101:  # 服务端对应的 Class 还没创建
                farmerSayItems = []
            else:
                raise e
        count = 0;
        resultItems = []
        for meetingItem in meetingItems:
            if(count == 1):
                break
            count +=1
            resultItem = {}
            resultItem['room'] = meetingItem.get('room')
            resultItem['orderman'] = meetingItem.get('orderman')
            resultItem['timefrom'] = meetingItem.get('timefrom')
            resultItem['timeto'] = meetingItem.get('timeto')
            resultItem['hc'] = meetingItem.get('hc')
            resultItem['building'] = meetingItem.get('building')
            resultItems.append(resultItem)
        data = {'code': 200, 'msg': 'successful', 'desc': "","meetingItems":resultItems}
        print "getStates: "
        return json.dumps(data, indent=1)
        pass
